package com.example.springbootvue.controller;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;

/**
 * 健康检查控制器
 * 用于监控应用程序状态
 */
@RestController
@RequestMapping("/api/health")
public class HealthController {

    /**
     * 检查系统状态
     * 
     * @return 系统状态信息
     */
    @GetMapping
    public ResponseEntity<Map<String, String>> health() {
        Map<String, String> status = new HashMap<>();
        status.put("status", "UP");
        status.put("timestamp", LocalDateTime.now().format(DateTimeFormatter.ISO_DATE_TIME));
        return ResponseEntity.ok(status);
    }

    /**
     * 详细的健康检查
     * 包含更多系统信息
     * 
     * @return 详细的系统状态信息
     */
    @GetMapping("/detail")
    public ResponseEntity<Map<String, Object>> healthDetail() {
        Map<String, Object> details = new HashMap<>();
        details.put("status", "UP");
        details.put("timestamp", LocalDateTime.now().format(DateTimeFormatter.ISO_DATE_TIME));

        // 添加系统信息
        Map<String, String> system = new HashMap<>();
        system.put("javaVersion", System.getProperty("java.version"));
        system.put("javaVendor", System.getProperty("java.vendor"));
        system.put("osName", System.getProperty("os.name"));
        system.put("osVersion", System.getProperty("os.version"));
        system.put("userTimezone", System.getProperty("user.timezone"));

        // 添加内存信息
        Map<String, String> memory = new HashMap<>();
        Runtime runtime = Runtime.getRuntime();
        long mb = 1024 * 1024;
        memory.put("freeMemory", (runtime.freeMemory() / mb) + " MB");
        memory.put("totalMemory", (runtime.totalMemory() / mb) + " MB");
        memory.put("maxMemory", (runtime.maxMemory() / mb) + " MB");

        details.put("system", system);
        details.put("memory", memory);

        return ResponseEntity.ok(details);
    }
}